package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FriendAdapter;
import com.iknow.util.Loger;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.ArrayList;
import java.util.List;

public class OnlineUserActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private View.OnClickListener ActionButtonClickListener;
  private View.OnClickListener BackButtonClickListener;
  private AdapterView.OnItemClickListener listItemClickListener;
  private Button mActionButton;
  private FriendAdapter mAdapter;
  private Button mBackButton;
  private boolean mBinded;
  private List<Friend> mFriendList;
  private boolean mInit;
  private MyListView mList;
  private ProgressDialog mProgressDialog;
  private ServiceConnection mSvrConn;
  private GetOnLineUserTask mTask;
  private TaskListener mTaskListener;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public OnlineUserActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.BackButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    };
    this.ActionButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        OnlineUserActivity.access$0(this.this$0);
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        Friend localFriend = OnlineUserActivity.access$1(this.this$0).getItem(paramInt);
        Intent localIntent = new Intent(this.this$0, FriendActivity.class);
        localIntent.putExtra("friend", localFriend);
        this.this$0.startActivity(localIntent);
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "GetOnLineUserTask";
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
        {
          OnlineUserActivity.access$1(this.this$0).clearFriend();
          OnlineUserActivity.access$1(this.this$0).setOnline(true);
          OnlineUserActivity.access$1(this.this$0).isShowFriendTags(true);
          OnlineUserActivity.access$1(this.this$0).setList(OnlineUserActivity.access$3(this.this$0));
          OnlineUserActivity.access$1(this.this$0).sortFriendList();
          OnlineUserActivity.access$1(this.this$0).notifyDataSetChanged();
          OnlineUserActivity.access$3(this.this$0).clear();
          OnlineUserActivity.access$4(this.this$0, true);
        }
        while (true)
        {
          if (OnlineUserActivity.access$6(this.this$0) != null)
            OnlineUserActivity.access$6(this.this$0).dismiss();
          return;
          Toast.makeText(this.this$0, OnlineUserActivity.access$5(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
        OnlineUserActivity.access$2(this.this$0, "正在获取在线好友，请稍候");
      }

      public void onProgressUpdate(, Object paramObject)
      {
      }
    };
  }

  private void getOnlineUser()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new GetOnLineUserTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  private void initView()
  {
    setContentView(2130903123);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(this.BackButtonClickListener);
    this.mActionButton = ((Button)findViewById(2131296344));
    this.mActionButton.setOnClickListener(this.ActionButtonClickListener);
    this.mActionButton.setText("换一批");
    this.mList = ((MyListView)findViewById(2131296466));
    this.mList.setAdapter(this.mAdapter);
    this.mList.setOnItemClickListener(this.listItemClickListener);
  }

  private void showProgressDialog(String paramString)
  {
    if (this.mProgressDialog == null)
      this.mProgressDialog = new ProgressDialog(this);
    if (this.mProgressDialog.isShowing())
      this.mProgressDialog.dismiss();
    this.mProgressDialog.setMessage(paramString);
    this.mProgressDialog.show();
  }

  protected void onCreate(Bundle paramBundle)
  {
    requestWindowFeature(1);
    super.onCreate(paramBundle);
    this.mAdapter = new FriendAdapter(this, getLayoutInflater());
    this.mFriendList = new ArrayList();
    initView();
  }

  protected void onDestroy()
  {
    this.mAdapter.clearFriend();
    this.mAdapter = null;
    super.onDestroy();
  }

  protected void onPause()
  {
    if (((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING)) || (this.mXmppFacade == null))
      this.mTask.cancel(true);
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    super.onPause();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    super.onResume();
  }

  private class GetOnLineUserTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      try
      {
        if ((OnlineUserActivity.access$9(this.this$0) == null) || (OnlineUserActivity.access$9(this.this$0).createConnection() == null) || (OnlineUserActivity.access$9(this.this$0).createConnection().getIKnowUser() == null))
        {
          this.mErrorMessage = "网络超时，稍后再试";
          localTaskResult = TaskResult.FAILED;
        }
        else if (OnlineUserActivity.access$9(this.this$0).createConnection().getIKnowUser().queryOnlineUsers())
        {
          OnlineUserActivity.access$3(this.this$0).addAll(IKnow.mIKnowDataBase.getNearByFriend());
          localTaskResult = TaskResult.OK;
        }
        else
        {
          this.mErrorMessage = "网络超时，稍后再试";
          localTaskResult = TaskResult.FAILED;
        }
      }
      catch (Exception localException)
      {
        while (true)
        {
          this.mErrorMessage = "网络超时，稍后再试";
          Loger.e("GetOnLineUserTask", localException.getMessage());
          localException.printStackTrace();
        }
      }
      return localTaskResult;
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      OnlineUserActivity.access$7(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      if (OnlineUserActivity.access$8(this.this$0));
      while (true)
      {
        return;
        OnlineUserActivity.access$0(this.this$0);
      }
    }

    public void onServiceDisconnected()
    {
      OnlineUserActivity.access$7(this.this$0, null);
    }
  }
}